/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SecureDelete;

import Catalano.Sanitization.Methods.AFSSI;
import Catalano.Sanitization.Methods.AR380;
import Catalano.Sanitization.Methods.DOD;
import Catalano.Sanitization.Methods.GOST;
import Catalano.Sanitization.Methods.Gutmann;
import Catalano.Sanitization.Methods.IProcess;
import Catalano.Sanitization.Methods.NAVSO;
import Catalano.Sanitization.Methods.Pfitzner;
import Catalano.Sanitization.Methods.RandomData;
import Catalano.Sanitization.Methods.Schneier;
import Catalano.Sanitization.Methods.WriteZero;
import Catalano.Sanitization.Tools.FileIO;
import Catalano.Sanitization.Tools.StringUtil;
import SecureDelete.Table.FileDescription;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Diego
 */
public class Work extends javax.swing.JFrame {
    
    private ArrayList<FileDescription> lst = new ArrayList<>();
    private int times;
    private int method;
    private boolean rename;
    
    private void CenterScreen(){
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = this.getSize().width;
        int h = this.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the window
        this.setLocation(x, y);
    }
    
    private void initConfig(){
        
        ClassLoader cl = this.getClass().getClassLoader();
        
        ImageIcon ico = new ImageIcon(cl.getResource("fire32.png"));
        this.setIconImage(ico.getImage());
        
        int size = lst.size();
        lblTotalFiles.setText(String.valueOf(size));
        lblCurrentFile.setText(lst.get(0).getPathname());
        lblRemainingFiles.setText(String.valueOf(size));
    }
    
    private void Process(){
        
        Task task = new Task();
        task.execute();
        
    }
    
    private IProcess getAlgorithm(int method){
        
        if (method == 0){
            return new WriteZero();
        }
        else if (method == 1){
            return new RandomData();
        }
        else if (method == 2){
            return new GOST();
        }
        else if (method == 3){
            return new DOD();
        }
        else if (method == 4){
            return new AR380();
        }
        else if (method == 5){
            return new AFSSI();
        }
        else if (method == 6){
            return new NAVSO();
        }
        else if (method == 7){
            return new Schneier();
        }
        else if (method == 8){
            return new Pfitzner();
        }
        else if (method == 8){
            return new Gutmann();
        }
        return new WriteZero();
        
    }
    
    public Work(){
        initComponents();
    }

    /**
     * Creates new form Work
     */
    public Work(ArrayList<FileDescription> lst, int method, int times, boolean rename){
        this.lst = lst;
        this.method = method;
        this.times = times;
        
        initComponents();
        CenterScreen();
        initConfig();
        Process();
    }
    
    class Task extends SwingWorker {

        public Task() {}

        @Override
        public Void doInBackground() throws InterruptedException{
                btnOK.setEnabled(false);
                double progress = 0;
                int filesP = 0;
                double part = 100 / (double)lst.size();
                while (filesP < lst.size()) {

                    //Delete file.
                    IProcess algorithm = getAlgorithm(method);
                    
                    String pathname = lst.get(filesP).getPathname();
                    
                    File f = new File(pathname);
                    if (f.exists()){
                        for (int i = 0; i < times; i++) {
                            algorithm.Process(pathname);
                        }

                        if (rename){
                            String str = StringUtil.generateWithExtension(32);
                            FileIO.renameFile(pathname, str);
                            pathname = str;
                        }

                        FileIO.delete(pathname);
                    }
                    progress+=part;
                    filesP++;
                    lblCurrentFile.setText(lst.get(filesP).getPathname());
                    lblRemainingFiles.setText(String.valueOf(lst.size() - filesP));
                    lblDeletedFiles.setText(String.valueOf(filesP + 1));
                    
                    
                    //Call the process method to update the GUI
                    publish((int)progress);

                }
                return null;
            }
        
        @Override
        protected void process(List chunks) {
            
         for (Object chunk : chunks) {
            progBar.setValue((Integer)chunk);
             
            if ((Integer)chunk > 49){
                progBar.setStringPainted(true);
            }
         }
     }
        
        //when the 'task' has finished re-enable the go button
        @Override
        public void done() {
            progBar.setValue(100);
            btnOK.setEnabled(true);
            lblRemainingFiles.setText("0");
            lblDeletedFiles.setText(String.valueOf(lst.size()));
            lblStatus.setText("Done");
            Main.CleanList();
            JOptionPane.showMessageDialog(null, "Done");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        progBar = new javax.swing.JProgressBar();
        btnOK = new javax.swing.JButton();
        lblStatus = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        lblTotalFiles = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblCurrentFile = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblRemainingFiles = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblDeletedFiles = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Working...");
        setResizable(false);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/work103.png"))); // NOI18N

        progBar.setStringPainted(true);

        btnOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ok24.png"))); // NOI18N
        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        lblStatus.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        lblStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblStatus.setText("Working...");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Summary"));

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        jLabel3.setText("Total files:");

        lblTotalFiles.setText("0");

        jLabel5.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        jLabel5.setText("Current file:");

        lblCurrentFile.setText("pathname");

        jLabel7.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        jLabel7.setText("Remaining files:");

        lblRemainingFiles.setText("0");

        jLabel9.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        jLabel9.setText("Deleted files:");

        lblDeletedFiles.setText("0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(lblCurrentFile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(18, 18, 18)
                                .addComponent(lblDeletedFiles))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(lblTotalFiles, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel7))
                                .addGap(18, 18, 18)
                                .addComponent(lblRemainingFiles)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblCurrentFile))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblTotalFiles))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(lblRemainingFiles))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(lblDeletedFiles))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(208, 208, 208)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(86, 86, 86)
                                .addComponent(progBar, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(211, 211, 211)
                                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 74, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(progBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnOKActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Work.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Work().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblCurrentFile;
    private javax.swing.JLabel lblDeletedFiles;
    private javax.swing.JLabel lblRemainingFiles;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTotalFiles;
    private javax.swing.JProgressBar progBar;
    // End of variables declaration//GEN-END:variables


}
